<?php

namespace app\api\controller;

use app\admin\model\Feature;
use app\common\controller\Api;
use Carbon\Carbon;

/**
 * 首页接口
 */
class Room extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function baseinfo()
    {
        $id = $this->request->get('id', null);
        $room = \app\common\model\Room::with('store')->find($id);
        /*
        if ($room->status != 'normal') {
            $this->error('该房间已下线');
        }
        */

        $room->room_bannerimage = url($room->room_bannerimage, '', false, true);
        $room->room_slideimages = (function ($value) {
            $rows = explode(',', $value);
            return array_map(function ($e) {
                return url($e, '', false, true);
            }, $rows);
        })($room->room_slideimages);

        $roomReservationCount = \app\admin\model\Reserve::where('room_id', $id)->where('starttime', '>', Carbon::now()->subMonth(3)->toDateTimeString())->count();
        $roomReservationCount = $roomReservationCount > 20 ? $roomReservationCount : '20+';

        $room->promotion_rent_info = "近期有{$roomReservationCount}次预定";
        $room->features = (function ($ids) {
            $features = Feature::whereIn('id', $ids)->select();
            return array_map(function ($e) {
                return [
                    'id' => $e->id,
                    'title' => $e->feature_name,
                    'color' => $e->color,
                    'icon_url' => url($e->iconimage, '', false, true),
                ];
            }, $features);
        })($room->feature_ids);
        $room->store_name = $room->store->store_name;

        $this->success('OK',  $room);
    }
}
